package bird.stack.array_stack;

import bird.stack.Stack;
import bird.stack.array_stack.Array;

/**
 * @author sijunqiang
 * @data 2021/5/22 17:21
 * @description: 自定义数组栈.底层的数据结构是数组
 */
public class ArrayStack<E> implements Stack<E> {

    private Array<E> array;

    public ArrayStack(int capacity) {
        array = new Array<>(capacity);
    }

    public ArrayStack() {
        array = new Array<>();
    }

    @Override
    public void push(E e) {
        array.addLast(e);
    }


    @Override
    public E pop() {
        // 对于栈来说 栈顶的元素 就是数组中最后一个元素。
        return array.removeLast();
    }

    @Override
    public E peek() {
        return array.getLast();
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public Boolean isEmpty() {
        return array.isEmpty();
    }

    public int getCapacity() {
        return array.getCapacity();
    }

    @Override
    public String toString() {
        // 自定义toString
        StringBuilder res = new StringBuilder();
        res.append("stack: ");
        res.append('[');
        for (int i = 0; i < array.getSize(); i++) {
            res.append(array.get(i));
            if (i != array.getSize() - 1) {
                res.append(",");
            }
        }
        res.append(']');
        res.append(" top");
        return res.toString();

    }
}
